package co.fitstart.admin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.demo2do.core.persistence.GenericDaoSupport;
import co.fitstart.admin.service.ResultReportService;
import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.course.round.CourseRoundResult;
import co.fitstart.entity.user.bwh.BwhRecord;

/**
 * 
 * @author haiyan_xiao
 *
 */
@Service("resultReportService")
public class ResultReportServiceImpl implements ResultReportService {

    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.ResultReportService#listBwhRecord(co.fitstart.entity.course.round.CourseRound)
     */
    @SuppressWarnings("unchecked")
    public List<BwhRecord> listBwhRecord(CourseRound courseRound) {
        return genericDaoSupport.searchForList("FROM BwhRecord WHERE disabled = false AND courseRound = :courseRound AND week IS NOT NULL", "courseRound", courseRound);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.ResultReportService#loadResultReport(co.fitstart.entity.course.round.CourseRound)
     */
    public CourseRoundResult loadResultReport(CourseRound courseRound) {
        return genericDaoSupport.searchForObject("FROM CourseRoundResult where courseRound = :courseRound", "courseRound", courseRound, CourseRoundResult.class);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.ResultReportService#getBwhRecordMap(co.fitstart.entity.course.round.CourseRound)
     */
    public Map<Integer, BwhRecord> getBwhRecordMap(CourseRound courseRound) {
        List<BwhRecord> bwhRecords = this.listBwhRecord(courseRound);
        
        if(bwhRecords.isEmpty()) {
            return null;
        } else {
            Map<Integer, BwhRecord> result = new HashMap<>();
            
            for(BwhRecord bwhRecord : bwhRecords) {
                result.put(bwhRecord.getWeek(), bwhRecord);
            }
            
            return result;
        }
    }

}
